package com.longrise.android.byjk.plugins.tabfirst.qrcode;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.DZZWTools;
import com.longrise.android.byjk.utils.Tracker_Google;

import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zbar.ZBarView;

/**
 * Created by callxkj on 16/8/4.
 * 二维码扫描界面
 */
public class QRcodeActivity extends BaseActivity implements QRCodeView.Delegate {

    private static final String TAG = "QRcodeActivity";
    private QRCodeView mQRCodeView;

    private Toolbar mTitle;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_qrcode;
    }


    public void initView() {
        mQRCodeView = (ZBarView) findViewById(R.id.zbarview);
        mQRCodeView.setDelegate(this);
        initTitle();
        addGoogleScreenName("二维码扫码");
    }

    private void initTitle() {
        mTitle = (Toolbar) findViewById(R.id.byjk_toolbar);
        mTitle.setTitle("二维码");
        setSupportActionBar(mTitle);
        mTitle.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        try {
            mQRCodeView.startCamera();
            mQRCodeView.showScanRect();
            mQRCodeView.startSpotDelay(500);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        mQRCodeView.stopCamera();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mQRCodeView.onDestroy();
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        parseQRCode(result);
    }

    @Override
    public void onScanQRCodeOpenCameraError() {

    }

    /**
     * 解析扫描二维码结果
     */
    private void parseQRCode(String result) {
        if (!TextUtils.isEmpty(result)) {
            if (result.startsWith("APPQrcode")) {
                toCheckifcanload(result);
            } else if (result.startsWith("http://")) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                Uri content_url = Uri.parse(result);
                intent.setData(content_url);
                startActivity(intent);
            }
        }
    }

    /**
     * 检查二维码是否过期
     *
     * @param
     */
    private void toCheckifcanload(final String qrCodeContent) {
        String userid = UserInfor.getInstance().getUserid();
        EntityBean bean = new EntityBean();
        bean.set("qrCodeContent", qrCodeContent);
        bean.set("userID", userid);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        mQRCodeView.hiddenScanRect();
        mQRCodeView.stopSpot();
        DZZWTools.showWaitDialog(this);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, "bbt_login_scanQRCode", fbean, new LoadDataManager.OnRequestCallListener() {
            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    Integer resultstate = bean.getInt("resultstate");
                    if (resultstate == 1) {
                        Intent intent = new Intent(QRcodeActivity.this, QRconfirmActivity.class);
                        intent.putExtra("qrCodeContent", qrCodeContent);
                        startActivity(intent);
                        finish();
                    } else {
                        String resultdesc = bean.getString("resultdesc");
                        DZZWTools.showToast(QRcodeActivity.this, resultdesc, 0);
                        decodeQRCodeFinish();
                    }
                } catch (Exception e) {
                    decodeQRCodeFinish();
                    DZZWTools.showToast(QRcodeActivity.this, "解析二维码失败", 0);
                }
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                decodeQRCodeFinish();
                DZZWTools.showToast(QRcodeActivity.this, "解析二维码失败", 0);
            }

            @Override
            public void onFinished(String key, String service) {
                decodeQRCodeFinish();
            }
        });
    }

    private void decodeQRCodeFinish() {
        DZZWTools.dismissDialog(null);
        mQRCodeView.showScanRect();
        mQRCodeView.startSpotDelay(500);
    }

    private void addGoogleScreenName(String name) {
        Tracker_Google.getInstance().addGoogleScreenName(name);
    }
}
